﻿/*
Customizer by Hoai Thanh Trinh
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using FShop.BAL;
using FShop.DAL;
public partial class EditCategory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		if ( !IsPostBack )
        {

            string category_ID = Request.QueryString["Category_ID"];

            if (ExtendFunctional.IsNumberic(category_ID) == false)
            {
                SetControlForInsert();
            }
            else
                LoadControlForEdit(category_ID);
        }
        else
        {
        
        }
    }
	#region For Set control when edit/insert
    private void SetControlForInsert()
    {
        btnAdd.Visible = true;
        btnEdit.Visible = false;
        //btnBack.Attributes.Add("onclick", "javascript :history.back(1)");
    }

    private void LoadControlForEdit(string category_ID)
    {
        Category category = Category_BAL.Get_Category_ByID(int.Parse(category_ID));
        if (category != null)
        {           
			//Gan control voi cac truong trong doi tuong
            txtCateLink.Value = category.Cate_Links;
            txtCateName.Value = category.Cate_Name;
            ddlCategory.SelectedIndex = category.Cate_ParentID-1;          
            
            chkStatus.Checked = category.Cate_Status;
        }
        btnAdd.Visible = false;
        btnEdit.Visible = true;
    }
    #endregion
	#region Insert/Update and upload image
    protected void Update()
    {
        string category_ID = Request.QueryString["Category_ID"];
        Category category = Category_BAL.Get_Category_ByID(int.Parse(category_ID));
		if (category != null)
        {
            category.Cate_Links= txtCateLink.Text;
            category.Cate_Name = txtCateName.Text;
            category.Cate_ParentID = int.Parse(ddlCategory.SelectedValue);       
            category.Cate_Status=chkStatus.Checked;
			Category_BAL.Update_Category(category);
		}
        
    }
    protected void Insert()
    {
        Category category = new Category();
		//Gan du lieu vao doi tuong	
        category.Cate_Links = txtCateLink.Text;
        category.Cate_Name = txtCateName.Text;
        category.Cate_ParentID = int.Parse(ddlCategory.SelectedValue);        
        category.Cate_Status = chkStatus.Checked;
        category.Cate_Image = "";
		//Them moi
		Category_BAL.Insert_Category(category);	
    }

    
    #endregion
	#region Button event

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            Insert();
            MessageBox.Show("Thêm danh mục thành công.");
            
        }
        catch(Exception ex)
        {
            MessageBox.Show("Thêm danh mục không thành công do đã có lỗi xảy ra.");
            throw ex;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            Update();
            MessageBox.Show("Sửa danh mục thành công.");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Sửa danh mục không thành công do đã có lỗi xảy ra.");
            throw ex;
        }
    } 
	#endregion
    protected void btnBack_ServerClick(object sender, EventArgs e)
    {
        Response.Redirect("ManageCategory.aspx");

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        txtCateName.Value = "a";
        Response.Redirect("ManageCategory.aspx");
    }
}

